﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@

@{
    //Layout = "~/Views/Shared/_FormLayout.cshtml";
    Layout = "~/Views/Shared/__ComponentLayout.cshtml";
    string editId = ViewData.Count > 0 ? ViewData["0"].ToString() : "null";
}
@using ST.Torch.WebApi.Web.Core;
@inherits UtilRazorPage<object>
<div style="padding: 16px;"></div>

<form style="padding: 16px;padding-right:30px" class="layui-form" action="" lay-filter="edit-data">
    <input type="text" name="Id" placeholder="" autocomplete="off" class="layui-input" style="display:none">
    <div class="layui-form-item">
        <div id="ID-tree-demo-showCheckbox"></div>
    </div>
   



    <div class="layui-form-item FormSubmit" style="text-align:right">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">@L("立即提交")</button>
            <button type="button" class="layui-btn layui-btn-primary" lay-filter="Close">@L("关闭")</button>

        </div>
    </div>
</form>
@section footer
 {
    <script>
        //Demo
        layui.use(['form', 'toast', 'tree'], function () {
            var form = layui.form;
            var toast = layui.toast;
            var tree = layui.tree;
            //var editId = '@GetValue("Id", "null")';
            var editId = '@editId';
            
            var initTree = function (Data) {
                // 渲染
                tree.render({
                    elem: '#ID-tree-demo-showCheckbox',
                    id: 'treeEle', // 自定义 id 索引
                    data: Data,
                   
                    showCheckbox: true,
                    // onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
                    // checkChildren: false,//是否与父选项联动
                    //edit: ['add', 'update', 'del'] // 开启节点的右侧操作图标
                });
            }
            Query();
            function GetChildrenId(Data) {
                var MenuIds = [];
                for (var i in Data) {
                    var itemD = Data[i];
                    MenuIds.push(itemD.id);
                    if (itemD.children.length > 0) {

                        var tempData = GetChildrenId(itemD.children);
                        for (var ii in tempData) {
                            MenuIds.push(tempData[ii]);
                        }

                    }
                }
                return MenuIds;
            }
            //监听按钮
            $(".layui-form button").click(function () {
                var id = $(this).attr('lay-filter');
                //alert(id);
                switch (id) {
                    case "Close": closeOpen(); break;
                }
            });
            //监听提交
            var buttonEle = null;
            form.on('submit(formDemo)', function (data) {
                console.log(data.field)
                console.log(tree)
                buttonEle = button.load({ elem: this });

                // 获取选中的节点数据
                var checkDatas = tree.getChecked('treeEle');
                var MenuIds = GetChildrenId(checkDatas)
                /*
                console.log("666----" + MenuIds)
                console.log(MenuIds)

                console.log(checkDatas)
                */
                Add(MenuIds);

                return false;
            });

            function Query() {
                console.log("--Query--")


                var formData = {
                    Id: editId
                };
                $.ajax({
                    type: 'POST',
                    url: '/api/SYSRole/GetRoleMenu',//发送请求
                    data: formData,
                    success: function (result) {
                        console.log(result)
                        if (result.Succeeded) {
                            initTree(result.Data)
                            //form.val("edit-data", result.Data);
                            //$('#ImgEle').attr('src', result.Data.Img); //图片链接（base64）


                        } else {
                            toast.error({
                                message: '@L("查询失败")',
                            });

                        }
                    }
                });

            }

            function Add(data) {
                var tempData = {
                    RoleId: editId,
                    MenuIds: data,

                }
                $.ajax({
                    type: 'POST',
                    url: '/api/SYSRole/InsertRoleMenu',//发送请求
                    data: tempData,
                    success: function (result) {
                        buttonEle.stop();
                        console.log(result)
                        if (result.Succeeded) {
                            toast.success({
                                message: '@L("授权成功")',
                            });
                            closeOpen();

                        } else {

                            toast.error({
                                message: '@L("添加失败")',
                            });

                        }
                    }, error: function (err) {
                        buttonEle.stop();
                    }
                });

            }

            function closeOpen() {
                parent.layer.close(G_LayuiOpenTypeIndex);
                return true;
            }
        });
    </script>
}